//
//  LogDetailViewController.m
//  StationManager
//
//  Created by 张佳强 on 18/5/12.
//  Copyright © 2018年 Helong. All rights reserved.
//

#import "LogDetailViewController.h"
#import "AFNwebManager.h"
#import <Masonry.h>
#import "LogSheetView.h"
#import "HuanjingImageCell.h"
#import "HuanjingImageModel.h"
#import <UIImageView+WebCache.h>

#define BLUECOLOR [UIColor colorWithRed:34/255.0 green:150/255.0 blue:243/255.0 alpha:1.0]

@interface LogDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UIScrollViewDelegate>

@property (nonatomic , strong) UIButton *yunweiButton;
@property (nonatomic , strong) UIButton *huanyingButton;
@property (nonatomic , strong) UIView *baseLine;
@property (nonatomic , strong) UIScrollView *scrollView;
@property (nonatomic , strong) LogSheetView *logSheetView;
@property (nonatomic , strong) UITableView *tableView;
//表格第一部分数组
@property (nonatomic , strong) NSArray *firstArray;
//表格左侧部分数组
@property (nonatomic , strong) NSMutableArray *sideArray;
//表格中间部分数组
@property (nonatomic , strong) NSMutableArray *middleArray;
//表格中间部分数据字典
@property (nonatomic , strong) NSMutableDictionary *allMiddleDict;
@property (nonatomic , strong) NSString *remark;
@property (nonatomic , strong) NSString *imageUrl;
@property (nonatomic , strong) UICollectionView *imageCollectionView;
@property (nonatomic , strong) NSMutableArray *imageDataArray;

@end

@implementation LogDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _imageDataArray = [NSMutableArray arrayWithCapacity:0];
    [[AFNwebManager shareAFNWebManager] SOAPData:@{@"Id":_logID} soapMethod:@"LogListStationImage" success:^(id responseObject) {
    
//        NSLog(@"%@",responseObject);
        NSArray *array = responseObject[@"data"];
        NSDictionary *dict = array[0];
        NSArray *urlArray = [dict[@"PicNameList"] componentsSeparatedByString:@"|"];
        NSArray *titleArray = [dict[@"PicTypeList"] componentsSeparatedByString:@"|"];
        
        if (urlArray.count == titleArray.count) {
            
            for (int i = 0; i<urlArray.count; i++) {
                
                HuanjingImageModel *model = [[HuanjingImageModel alloc] init];
                model.title = titleArray[i];
                model.imageUrl = urlArray[i];
                [_imageDataArray addObject:model];
            }
        }
        
        [_imageCollectionView reloadData];
    }failure:^{
        
    }];
        
        
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"详情查看";
    NSLog(@"type:%@",_type);
    _remark = @"";
    _imageUrl = @"";
    _sideArray = [NSMutableArray arrayWithCapacity:0];
    _allMiddleDict = [NSMutableDictionary dictionaryWithCapacity:0];
    [[AFNwebManager shareAFNWebManager] SOAPData:@{@"Id":_logID} soapMethod:@"LogListDetail" success:^(id responseObject) {
        
        NSArray *array = responseObject[@"data"];
        NSDictionary *dataDict = array[0];
        NSArray *k1Array = dataDict[@"k1"];
        NSDictionary *k1Dict = k1Array[0];
        
        
        _firstArray = @[@"台站名称",k1Dict[@"StationName"],@"台站号",k1Dict[@"StationID"],@"维护日期",k1Dict[@"WorkTime"],@"维护人员",k1Dict[@"WorkRenYuans"],@"运维项目",@"先前状态",@"运维措施",@"运维结果"];
        _remark = k1Dict[@"Remarks"];
        _imageUrl = k1Dict[@"StationImage"];
        [_sideArray addObjectsFromArray:dataDict.allKeys];
//        if ([_type isEqualToString:@"T4"]) {
//            
//        }
        [_sideArray removeObject:@"k1"];
      

        for (NSString *keys in _sideArray) {
              _middleArray = [NSMutableArray arrayWithCapacity:0];
            NSArray *eachArray = dataDict[keys];

            for (NSDictionary *subDict in eachArray) {
                
                
                    
                [_middleArray addObject:subDict[@"Content"]];
                 [_middleArray addObject:subDict[@"Statue"]];
                [_middleArray addObject:subDict[@"Measure"]];
                [_middleArray addObject:subDict[@"Result"]];

            }
            [_allMiddleDict setObject:_middleArray forKey:keys];


        }
        [_sideArray addObject:@"其他"];

        NSString *huanjing = k1Dict[@"HuanJing"];
//        NSArray *huanjingArray = [huanjing componentsSeparatedByString:@"|"];
        NSMutableArray *huanjingArray = [NSMutableArray arrayWithCapacity:0];
        
        [huanjingArray addObject:@"运维环境"];
        [huanjingArray addObjectsFromArray:[huanjing componentsSeparatedByString:@"|"]];
        [_allMiddleDict setObject:huanjingArray forKey:@"其他"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView reloadData];
        
    } failure:^{
        
    }];
    
    [self setUI];
}


-(void)setUI{
    
    _yunweiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_yunweiButton setTitle:@"运维日志" forState:UIControlStateNormal];
    [_yunweiButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_yunweiButton setTitleColor:BLUECOLOR forState:UIControlStateSelected];
    _yunweiButton.selected = YES;
    _yunweiButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_yunweiButton addTarget:self action:@selector(yunweiClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_yunweiButton];
    
    [_yunweiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDTH/2);
        make.height.mas_equalTo(40);
    }];
    
    _huanyingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_huanyingButton setTitle:@"探测环境" forState:UIControlStateNormal];
    [_huanyingButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_huanyingButton setTitleColor:BLUECOLOR forState:UIControlStateSelected];
    _huanyingButton.selected = NO;
    _huanyingButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_huanyingButton addTarget:self action:@selector(huangjingClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_huanyingButton];
    
    [_huanyingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.top.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDTH/2);
        make.height.mas_equalTo(40);
    }];
    
    _baseLine = [[UIView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH/2, 3)];
    _baseLine.backgroundColor = BLUECOLOR;
    [self.view addSubview:_baseLine];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 43, SCREEN_WIDTH, 1)];
    line.backgroundColor = BLUECOLOR;
    [self.view addSubview:line];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 45, SCREEN_WIDTH, self.view.frame.size.height-45)];
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*2, self.view.frame.size.height-45);
    _scrollView.bounces = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-20) style:UITableViewStylePlain];

    _tableView.showsVerticalScrollIndicator = NO;
    [_scrollView addSubview:_tableView];
    
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _imageCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH,  SCREEN_HEIGHT-110)collectionViewLayout:flow];
    _imageCollectionView.backgroundColor = [UIColor whiteColor];
    _imageCollectionView.delegate = self;
    _imageCollectionView.dataSource = self;
    _imageCollectionView.bounces = YES;
    _imageCollectionView.pagingEnabled = YES;
    [_imageCollectionView registerClass:[HuanjingImageCell class] forCellWithReuseIdentifier:@"huanjingImageCell"];
    
    [_scrollView addSubview:_imageCollectionView];
    
 
    
 
}

-(void)yunweiClick{
    
    
    _yunweiButton.selected = YES;
    _huanyingButton.selected = NO;
    
    [UIView animateWithDuration:0.2 animations:^{
        self.scrollView.contentOffset = CGPointMake(0, 0);
        _baseLine.frame = CGRectMake(0, 40, SCREEN_WIDTH/2, 3);
    }];

}


-(void)huangjingClick{
    
    _yunweiButton.selected = NO;
    _huanyingButton.selected = YES;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);

        _baseLine.frame = CGRectMake(SCREEN_WIDTH/2, 40, SCREEN_WIDTH/2, 3);
    }];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    if (scrollView == _scrollView) {
        int i = scrollView.contentOffset.x/SCREEN_WIDTH;
        if (i == 0) {
            _yunweiButton.selected = YES;
            _huanyingButton.selected = NO;
            [UIView animateWithDuration:0.1 animations:^{
                _baseLine.frame = CGRectMake(0, 40, SCREEN_WIDTH/2, 3);
            }];
            
        }else{
            _yunweiButton.selected = NO;
            _huanyingButton.selected = YES;
            
            [UIView animateWithDuration:0.1 animations:^{
                _baseLine.frame = CGRectMake(SCREEN_WIDTH/2, 40, SCREEN_WIDTH/2, 3);
            }];
        }
  
    }
  
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return 1;


}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        static NSString *iden = @"sheetCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
        _logSheetView = [[LogSheetView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height-45)];
        _logSheetView.firstArray = _firstArray;
        _logSheetView.sideArray = _sideArray;
        _logSheetView.dataDict = _allMiddleDict;
        _logSheetView.remark = _remark;
        _logSheetView.imageUrl = _imageUrl;
        [_logSheetView setSheetView];
        [cell.contentView addSubview:_logSheetView];
        
        return cell;


}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        return 850;

 
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return _imageDataArray.count;
        
  
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 1;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 1;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
        return UIEdgeInsetsMake(0,0,0,0);
        
  
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    

     return CGSizeMake(SCREEN_WIDTH,  SCREEN_HEIGHT-110);
      
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HuanjingImageCell * cell = (HuanjingImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"huanjingImageCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    HuanjingImageModel *model = _imageDataArray[indexPath.item];
    cell.titleLabel.text = model.title;
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];
    return cell;
  
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
